package com.prs.crm.action.pm;

import java.io.File;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.prs.crm.action.FlowAction;
import com.prs.crm.annotation.Clew;
import com.prs.crm.annotation.FlowActive;
import com.prs.crm.annotation.FlowStart;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Constrains;
import com.prs.crm.context.ToolKits;
import com.prs.crm.domain.doc.DocLevel;
import com.prs.crm.domain.doc.Document;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.domain.pm.PmDoc;
import com.prs.crm.domain.pm.PmGroup;
import com.prs.crm.domain.pm.PmProject;
import com.prs.crm.domain.pm.PmSource;
import com.prs.crm.domain.pm.PmType;
import com.prs.crm.service.doc.DocLevelService;
import com.prs.crm.service.doc.DocumentService;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.pm.PmProjectService;
import com.prs.crm.service.pm.PmSourceService;
import com.prs.crm.service.pm.PmTypeService;

public class PmProjectAction extends FlowAction<PmProject>{
	private PmProject pmProject;
	private Document document;
	private PmProjectService pmProjectService;
	private DocumentService documentService;
	private PmTypeService pmTypeService;
	private PmSourceService pmSourceService;
	private DocLevelService docLevelService;
	private PersonService personService;
	private PaginationSupport pagination;
	private Integer[] ids;
	private File upload;
	private String uploadFileName;
	
	private String manageGroup;
	private String memberGroup;
	private String manageGroup_names;
	private String memberGroup_names;
	private String pmBudget;
	
	private Collection<Document> documents;
	private Collection<PmType> pmTypes;
	private Collection<PmSource> pmSources;
	private Collection<DocLevel> docLevels;

	
	//创建项目
	@Action("listCreateProject")
	public String listCreateProject() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(PmProject.class);
		criteria.add(Restrictions.eq("recorder", AppContext.getAccountContext().currentUser()));
		criteria.add(Restrictions.eq("state", Constrains.PM_STATE_EDIT));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPmProjectService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	//保存项目，项目状态为编辑状态，可以进行继续编辑或删除
	@Action(value="savePmProject",results={@Result(name="success",type="redirect",location="listCreateProject"),
			@Result(name="error",type="chain",location="editPmProject")})
	public String savePmProject() {
		if(pmProject.getTitle() == null || pmProject.getTitle().trim().equals("")) {
			this.addActionError("请正确填写项目名称");
			return ERROR;
		}
		if(pmProject.getStartTime() == null || pmProject.getEndTime() == null) {
			this.addActionError("请设置项目开始/结束时间");
			return ERROR;
		}
		if(pmProject.getPrincipal() == null) {
			this.addActionError("请设置项目负责人");
			return ERROR;
		}
		if(manageGroup == null || memberGroup == null) {
			this.addActionError("项目管理组与项目成员组不能为空");
			return ERROR;
		}
		if(!ToolKits.isNumeric(pmBudget)) {
			this.addActionError("项目预算应为数字型");
			return ERROR;
		}
		
		pmProject.setState(Constrains.PM_STATE_EDIT);
		pmProject.setBudget(Double.parseDouble(pmBudget));
		this.getPmProjectService().submitProject(pmProject, upload, uploadFileName,memberGroup,manageGroup);
		return SUCCESS;
	}
	
	//创建、编辑项目
	@FlowActive(target="pmProject",flowservice="pmProjectService")
	@Action("editPmProject")
	public String editPmProject() {
		if(pmProject != null && pmProject.getId() != null) {
			this.setDocuments(this.getDocumentService().getProjectCreateDoc(pmProject));
			
			manageGroup_names=null;
			memberGroup_names=null;
			manageGroup = null;
			memberGroup = null;
			for(Person person : pmProject.getManageGroup()) {
				manageGroup_names = (manageGroup_names==null)?person.getResume().getName():manageGroup_names+","+person.getResume().getName();
				manageGroup = (manageGroup==null)?person.getId().toString():manageGroup+","+person.getId().toString();
			}
			for(PmGroup group : pmProject.getMemberGroup()) {
				memberGroup_names = (memberGroup_names == null)?group.getName():memberGroup_names+","+group.getName();
				memberGroup=(memberGroup==null)?group.getId().toString():memberGroup+","+group.getId().toString();
			}
			this.pmBudget = String.valueOf(pmProject.getBudget());
		}else {
			this.setDocuments(new ArrayList<Document>());
		}
		this.setPmTypes(this.getPmTypeService().getAllPmType());
		this.setPmSources(this.getPmSourceService().getAllPmSource());
		return SUCCESS;
	}
	
	//创建项目文档
	@Action("editPmDoc")
	public String editPmDoc() {
		this.setDocLevels(this.getDocLevelService().getAllDocLevel());
		return SUCCESS;
	}
	
	@Action(value="saveProjectCreateDoc",results={@Result(name="success",type="redirect",location="editPmDoc?pmProject=${pmProject.id}"),
			@Result(name="error",type="chain",location="editPmProject")})
	public String saveProjectCreateDoc() {		
		return this.savePmProject();
	}	
	
	//提交项目文档
	@Action(value="submitPmDoc",results={@Result(name="success",type="redirect",location="editPmProject?pmProject=${pmProject.id}"),
			@Result(name="error",type="chain",location="editPmDoc")})
	public String submitPmDoc() {
		if(document.getName() == null || document.getName().trim().equals("")) {
			this.addActionError("请正确填写文档名称");
			return ERROR;
		}
		if (document.getDocumentcode() == null || document.getDocumentcode().trim().equals("")) {
			this.addActionError("请正确填写文档编码");
			return ERROR;
		}
		document.setProject(pmProject);
		this.getDocumentService().submitDocument(document);//, upload, uploadFileName);
		return SUCCESS;
	}
	
	//删除项目文档
	@Action(value="removePmDoc",results={@Result(name="success",type="redirect",location="editPmProject?pmProject=${pmProject.id}"),
			@Result(name="error",type="chain",location="editPmProject")})
	public String removePmDoc() {
		if(ids == null) {
			this.addActionError("请选择要删除的文档");
			return ERROR;
		}
		this.getDocumentService().batchRemove(ids);
		return SUCCESS;
	}
	
	//删除项目
	@Action(value="removePmProject",results={@Result(name="success",type="redirect",location="listCreateProject"),
			@Result(name="error",type="chain",location="listCreateProject")})
	public String removePmProject() {
		if(ids == null) {
			this.addActionError("请选择要删除的项目");
			return ERROR;
		}
		this.getPmProjectService().batchRemove(ids);
		return SUCCESS;
	}
	
	//浏览项目文档
	@Action("viewPmDoc")
	public String viewPmDoc() {
		return SUCCESS;
	}
	
	//提交项目，项目状态为初始化，可进行项目阶段设置
	@Clew(results={"success"})
	@FlowStart(target="pmProject",flowservice="pmProjectService")
	@Action(value="submitPmProject",results={@Result(name="success",type="redirect",location="editPmProject"),
			@Result(name="error",type="chain",location="editPmProject")})
	public String submitPmProject() {
		if(pmProject.getTitle() == null || pmProject.getTitle().trim().equals("")) {
			this.addActionError("请正确填写项目名称");
			return ERROR;
		}
		if(pmProject.getStartTime() == null || pmProject.getEndTime() == null) {
			this.addActionError("请设置项目开始/结束时间");
			return ERROR;
		}
		if(pmProject.getPrincipal() == null) {
			this.addActionError("请设置项目负责人");
			return ERROR;
		}
		if(manageGroup == null || memberGroup == null) {
			this.addActionError("项目管理组与项目成员组不能为空");
			return ERROR;
		}
		if(!ToolKits.isNumeric(pmBudget)) {
			this.addActionError("项目预算应为数字型");
			return ERROR;
		}		
		
		pmProject.setState(Constrains.PM_STATE_INIT);
		pmProject.setBudget(Double.parseDouble(pmBudget));
		
		this.getPmProjectService().submitProject(pmProject, upload, uploadFileName,memberGroup,manageGroup);
		this.setPrompt("创建项目成功");
		return SUCCESS;
	}	
	
	//初始化项目
	@Action("listInitProject")
	public String listInitProject() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(PmProject.class);
		criteria.add(Restrictions.eq("principal", AppContext.getAccountContext().currentUser()));
		criteria.add(Restrictions.eq("state", Constrains.PM_STATE_INIT));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPmProjectService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("listExecuteProject")
	public String listExecuteProject() {
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(PmProject.class);
		criteria.add(Restrictions.eq("principal", AppContext.getAccountContext().currentUser()));
		criteria.add(Restrictions.eq("state", Constrains.PM_STATE_START));
		criteria.addOrder(Order.desc("recordTime"));
		pagination = this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getPmProjectService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("executeProject")
	public String executeProject() {
		return SUCCESS;
	}

	public PmProject getPmProject() {
		return pmProject;
	}
	public void setPmProject(PmProject pmProject) {
		this.pmProject = pmProject;
	}
	public PmProjectService getPmProjectService() {
		return pmProjectService;
	}
	public void setPmProjectService(PmProjectService pmProjectService) {
		this.pmProjectService = pmProjectService;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public File getUpload() {
		return upload;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setManageGroup(String manageGroup) {
		this.manageGroup = manageGroup;
	}

	public String getManageGroup() {
		return manageGroup;
	}

	public void setMemberGroup(String memberGroup) {
		this.memberGroup = memberGroup;
	}

	public String getMemberGroup() {
		return memberGroup;
	}
	
	public PmTypeService getPmTypeService() {
		return pmTypeService;
	}

	public void setPmTypeService(PmTypeService pmTypeService) {
		this.pmTypeService = pmTypeService;
	}

	public PmSourceService getPmSourceService() {
		return pmSourceService;
	}

	public void setPmSourceService(PmSourceService pmSourceService) {
		this.pmSourceService = pmSourceService;
	}

	public Collection<PmType> getPmTypes() {
		return pmTypes;
	}

	public void setPmTypes(Collection<PmType> pmTypes) {
		this.pmTypes = pmTypes;
	}

	public Collection<PmSource> getPmSources() {
		return pmSources;
	}

	public void setPmSources(Collection<PmSource> pmSources) {
		this.pmSources = pmSources;
	}

	public DocLevelService getDocLevelService() {
		return docLevelService;
	}

	public void setDocLevelService(DocLevelService docLevelService) {
		this.docLevelService = docLevelService;
	}

	public Collection<DocLevel> getDocLevels() {
		return docLevels;
	}

	public void setDocLevels(Collection<DocLevel> docLevels) {
		this.docLevels = docLevels;
	}

	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public PersonService getPersonService() {
		return personService;
	}

	public void setPmBudget(String pmBudget) {
		this.pmBudget = pmBudget;
	}

	public String getPmBudget() {
		return pmBudget;
	}

	public String getManageGroup_names() {
		return manageGroup_names;
	}

	public void setManageGroup_names(String manageGroupNames) {
		manageGroup_names = manageGroupNames;
	}

	public String getMemberGroup_names() {
		return memberGroup_names;
	}

	public void setMemberGroup_names(String memberGroupNames) {
		memberGroup_names = memberGroupNames;
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}

	public DocumentService getDocumentService() {
		return documentService;
	}

	public void setDocuments(Collection<Document> documents) {
		this.documents = documents;
	}

	public Collection<Document> getDocuments() {
		return documents;
	}
}
